package com.aiju.cash.ui.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.AccountApi;
import com.aiju.cash.core.event.AccountEvent;
import com.aiju.cash.ui.account.entity.ItemList;
import com.aiju.cash.util.GsonHelper;
import com.aiju.common.PixValue;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.MemberDialog;
import com.aiju.widgit.layout.SettingItemTextView;

/**
 * @author: 佐为
 * @date: 2017-03-29 14:43
 * @version: V1.0
 * @Description: 账目详情
 */
public class AccountItemInfoActivity extends BaseTitleActivity {

    private SettingItemTextView timeTextView;
    private SettingItemTextView noterTextView;
    private SettingItemTextView remarkTextView;
    private SettingItemTextView typeTextView;

    private ItemList.Item item;
    private TextView budgetMoneyTextView, budgetTypeTextView, budgetCategoryTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_item_info);
        setCenterTitle("账目详情");
        item = (ItemList.Item) getIntent().getSerializableExtra("item");

        budgetMoneyTextView = (TextView) findViewById(R.id.tv_budget_money);
        budgetTypeTextView = (TextView) findViewById(R.id.tv_budget_type);
        budgetCategoryTextView = (TextView) findViewById(R.id.tv_budget_category);

        timeTextView = (SettingItemTextView) findViewById(R.id.set_item_time);
        noterTextView = (SettingItemTextView) findViewById(R.id.set_item_noter);
        typeTextView = (SettingItemTextView) findViewById(R.id.set_item_type);
        remarkTextView = (SettingItemTextView) findViewById(R.id.set_item_remark);
        remarkTextView.getRightView().setPadding(PixValue.dp.valueOf(15), PixValue.dp.valueOf(12), PixValue.dp.valueOf(15), PixValue.dp.valueOf(12));

        findViewById(R.id.tv_item_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AccountItemInfoActivity.this, AccountCreatActivity.class);
                intent.putExtra("item", item);
                intent.putExtra("showType", AccountCreatActivity.EDIT);
                startActivity(intent);
            }
        });
        findViewById(R.id.tv_item_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new MemberDialog(AccountItemInfoActivity.this)
                        .setTitileText("是否删除'" + item.getCategoryName() + "'账目")
                        .setMsgText("改数据将会删除并不可恢复，请谨慎操作！")
                        .setRightText("删除")
                        .setLayoutParams()
                        .setOnSimpleClick(new MemberDialog.OnSimpleClick() {
                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onSubmit() {
                                requsetItemDelete(item);
                            }
                        })
                        .show();
            }
        });

        requsetItemInfo();
    }

    /**
     * 获取详细
     */
    private void requsetItemInfo() {
        new AccountApi(mDialogHelper).itemGet(item.getId(), new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                item = GsonHelper.convertEntity(data, ItemList.Item.class);
                budgetMoneyTextView.setText(item.getSimpleMoney());
                budgetTypeTextView.setText(item.getTypeString());
                budgetCategoryTextView.setText(item.getCategoryName());

                timeTextView.setRightText(item.getModifyDate());
                noterTextView.setRightText(item.getOperatorName());
                typeTextView.setRightText(item.getAccountType());
                remarkTextView.setRightText(item.getRemark());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        requsetItemInfo();
    }

    /**
     * 删除账目
     *
     * @param item
     */
    private void requsetItemDelete(ItemList.Item item) {
        new AccountApi(mDialogHelper).itemDelete(item.getId(), new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                EventCenter.getInstance().post(new AccountEvent(1));
                T.ToastShort("账目删除成功");
                finish();
            }
        });
    }
}
